package com.itlong.cloud.intercept.sdkV3;

import com.itlong.cloud.POJO.PO.PropertyUserInfoPO;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.noncore.commons.IOperationLogServiceClient;
import com.itlong.cloud.user.sdk.ICloudTalkSDKAppUserServiceClient;
import com.itlong.cloud.user.sdk.ISDKServerOldUserServiceClient;
import com.itlong.cloud.utils.date.DateUtil;
import com.itlong.cloud.utils.encrypt.DesEncryptUtil;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.node.JsonUtil;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <desc>
 * SDK开发者日志记录器
 * </desc>
 *
 * @createDate 2019/09/18
 */
@Aspect
@Component
public class SDKLoggerInterceptor {

    // 定义一个切入点
    @Pointcut("execution(* com.itlong.cloud.controller.sdkV3..*.*(..))")
    public void excudeService() {
    }

    @Autowired
    IOperationLogServiceClient iOperationLogServiceClient;

    @Autowired
    ISDKServerOldUserServiceClient iSDKUserServiceClient;

    @Autowired
    ICloudTalkSDKAppUserServiceClient iCloudTalkSDKAppUserServiceClient;

    /**
     * <desc>
     * 开发者访问日志拦截器
     * </desc>
     *
     * @param
     * @author Qiang.S
     * @createDate 2019/09/18
     */
    @After("excudeService() && @annotation(loggerInfo)")
    public void twiceAsOld(LoggerInfo loggerInfo) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = HttpProtocolUtil.getIpAddr(request);
        String token = request.getParameter("token");
        PropertyUserInfoPO propertyUserInfoPO;
        if (StringUtils.isBlank(token)) {
            propertyUserInfoPO = iSDKUserServiceClient.getPropertyUserByAppId(request.getParameter("appId"));
        }else {
            propertyUserInfoPO = iCloudTalkSDKAppUserServiceClient.developerRetrieve(token);
        }
        Map<String, Object> map = new HashMap<>();

        // 正常保存访问日志
        if (propertyUserInfoPO != null && request.getAttribute("exception") == null) {
            map.put("logId", LogicIdUtil.bussinessId());
            map.put("account", propertyUserInfoPO.getUserName());
            map.put("projectId", request.getAttribute("projectId"));
            map.put("portType", request.getAttribute("portType"));// 接口类别
            map.put("developerUse", loggerInfo.operateDesc());
            map.put("remark", request.getAttribute("remark"));// 详细描述
            map.put("ip", ip);
            map.put("ipAddress", HttpProtocolUtil.getIpAddressFromBaidu(ip));
            map.put("requestId", request.getAttribute("requestId"));
            map.put("timestamp", request.getAttribute("timestamp"));
            map.put("version", request.getAttribute("version"));
            map.put("status", DataStatusEnum.NORMAL.getType());
            Date nowTime = new Date();
            String timeStr = new SimpleDateFormat(DateUtil.DATE_FORMAT_TYPE1).format(nowTime);
            map.put("createTime", timeStr);
            map.put("updateTime", timeStr);
            try {
                iOperationLogServiceClient.saveDeveloperLog(map);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
